#include "mainframe.h"
#include <QtGui/QApplication>

#ifdef WIN32

//dump minidump in windows environment
#include <DbgHelp.h>
#pragma comment(lib,"DbgHelp.lib")

INT write_dump(PEXCEPTION_POINTERS exceptPtr)
{
	if ( !exceptPtr ) { return 0; }

	const int TEMPSTRLEN=256;
	char filename[TEMPSTRLEN];
	::ZeroMemory(filename,TEMPSTRLEN);
	SYSTEMTIME sysTime;
	::ZeroMemory(&sysTime,sizeof(sysTime));
	::GetLocalTime(&sysTime);
	sprintf_s(filename,TEMPSTRLEN,"./guistudio_memdump_%d.%d.%d_%d.%d.%d.dmp",
		sysTime.wYear,sysTime.wMonth,sysTime.wDay,
		sysTime.wHour,sysTime.wMinute,sysTime.wSecond);

	MINIDUMP_EXCEPTION_INFORMATION exceptInfo;
	::ZeroMemory(&exceptInfo,sizeof(exceptInfo));
	exceptInfo.ExceptionPointers = exceptPtr;
	exceptInfo.ClientPointers = FALSE;
	exceptInfo.ThreadId = ::GetCurrentThreadId();

	HANDLE hFile=::CreateFileA(filename,GENERIC_WRITE,0,0,CREATE_NEW,FILE_ATTRIBUTE_COMPRESSED,0);
	::MiniDumpWriteDump(
		::GetCurrentProcess(),
		::GetCurrentProcessId(),
		hFile,
		MiniDumpNormal,
		&exceptInfo,NULL,NULL);
	::CloseHandle(hFile);
	return 0;
}
#endif

//proxy main
int proxy_main(int argc,char **argv)
{
	QApplication a(argc, argv);
	MainFrame w;
	w.show();
	return a.exec();
}

//real main entry
int main(int argc, char **argv)
{
	int ret = 0;
#ifdef WIN32
	__try
	{
#endif
		ret = proxy_main(argc,argv);
#ifdef WIN32
	}__except(write_dump(GetExceptionInformation()))
	{
		//we got an exception!
		return -1;
	}
#endif

	return ret;
}
